package com.ruoyi.event.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.event.domain.EventWfxy;
import com.ruoyi.event.domain.bo.EventWfxyBo;
import com.ruoyi.event.domain.vo.EventWfxyVo;
import com.ruoyi.event.mapper.EventWfxyMapper;
import com.ruoyi.event.service.IEventWfxyService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 违反协议Service业务层处理
 *
 * @author strongme
 * @date 2023-02-04
 */
@RequiredArgsConstructor
@Service
public class EventWfxyServiceImpl implements IEventWfxyService {

    private final EventWfxyMapper baseMapper;

    /**
     * 查询违反协议
     */
    @Override
    public EventWfxyVo queryById(Long wfxyId) {
        return baseMapper.selectVoById(wfxyId);
    }

    /**
     * 查询违反协议列表
     */
    @Override
    public TableDataInfo<EventWfxyVo> queryPageList(EventWfxyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EventWfxy> lqw = buildQueryWrapper(bo);
        Page<EventWfxyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询违反协议列表
     */
    @Override
    public List<EventWfxyVo> queryList(EventWfxyBo bo) {
        LambdaQueryWrapper<EventWfxy> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EventWfxy> buildQueryWrapper(EventWfxyBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EventWfxy> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, EventWfxy::getUserId, bo.getUserId());
        lqw.eq(bo.getWfxysj() != null, EventWfxy::getWfxysj, bo.getWfxysj());
        lqw.eq(StringUtils.isNotBlank(bo.getWfxylx()), EventWfxy::getWfxylx, bo.getWfxylx());
        lqw.eq(StringUtils.isNotBlank(bo.getWfxyss()), EventWfxy::getWfxyss, bo.getWfxyss());
        lqw.eq(StringUtils.isNotBlank(bo.getClqk()), EventWfxy::getClqk, bo.getClqk());
        lqw.eq(StringUtils.isNotBlank(bo.getQjs()), EventWfxy::getQjs, bo.getQjs());
        return lqw;
    }

    /**
     * 新增违反协议
     */
    @Override
    public Boolean insertByBo(EventWfxyBo bo) {
        EventWfxy add = BeanUtil.toBean(bo, EventWfxy.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setWfxyId(add.getWfxyId());
        }
        return flag;
    }

    /**
     * 修改违反协议
     */
    @Override
    public Boolean updateByBo(EventWfxyBo bo) {
        EventWfxy update = BeanUtil.toBean(bo, EventWfxy.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EventWfxy entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除违反协议
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
